package dataClean;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;


/**
 * @author Smartloe
 * @create 2021-04-27 16:39
 */
public class TxtCase {
    public static void main(String[] args) throws Exception {
        //求平均分数 求最大数 求最小数 求中位数 求标准差
        score();
        //时间数据提取
        timetransform();
    }

    public static void score() throws FileNotFoundException {
        //step 1 打开文件并且读取内容。
        Scanner file = new Scanner(new File("C:\\Users\\25392\\Desktop\\Java基础\\src\\dataClean\\data\\TxtCase1.txt"));

        ArrayList<Double> fileContent = new ArrayList<Double>();
        //移除文件中一部分的字符
        double score;
        String clearString;
        while (file.hasNext()) {
            clearString = file.next();
            System.out.println(clearString);
            clearString = clearString.replaceAll("score:", "");
            score = Double.parseDouble(clearString);//数据类型转换
            fileContent.add(score);
        }
        //setp 2 输出文件内容
        //计算分数平均值
        double averageOfscore = 0, sumOfScore = 0;
        int amount = 0;

        for (Double scoreData : fileContent) {
            amount++;
            sumOfScore += scoreData;
        }
        averageOfscore = sumOfScore / amount;
        System.out.println("一共有" + amount + "条成绩数据\n平均分数为：" + averageOfscore);
        //求最大数
        double maxValue = fileContent.get(0);
        for (int i = 0; i < amount; i++) {
            if (maxValue < fileContent.get(i)) {
                maxValue = fileContent.get(i);
            }
        }
        System.out.println("最大值:\t" + maxValue);
        //求最小数
        double minValue = fileContent.get(0);
        for (int i = 0; i < amount; i++) {
            if (minValue > fileContent.get(i)) {
                minValue = fileContent.get(i);
            }
        }
        System.out.println("最小值:\t" + minValue);
        //求中位数
        double[] numbers = new double[amount];
        for (int i = 0; i < amount; i++) {
            numbers[i] = fileContent.get(i);
        }
        Arrays.sort(numbers);
        double median;
        int mid;
        if (amount % 2 == 0) {
            mid = amount / 2;
            median = (numbers[mid] + numbers[mid - 1]) / 2.0;
        } else {
            mid = (amount - 1) / 2;
            median = numbers[mid];
        }
        System.out.println("中位数：\t" + median);
        //求标准差
        double sum = 0, std;
        for (int i = 0; i < amount; i++) {
            sum += (numbers[i] + averageOfscore) * (numbers[i] + averageOfscore);
        }
        std = Math.sqrt(sum / amount);
        System.out.println("标准差：\t" + std);
    }

    public static void timetransform() throws FileNotFoundException {
        //step 1 打开文件并且读取内容。
        Scanner file = new Scanner(new File("C:\\Users\\25392\\Desktop\\Java基础\\src\\dataClean\\data\\TxtCase2.txt"));
        ArrayList<String> fileText = new ArrayList<String>();
        //移除文件中一部分的字符
        String time;
        String clearString;
        System.out.println("\n清洗前的数据：");
        while (file.hasNext()) {
            clearString = file.next();
            System.out.println(clearString);
            clearString = clearString.replaceAll("time:", "");
            time = clearString;
            fileText.add(time);
        }
        System.out.println("清洗后的数据：");
         int counts = 0;
        for (int i = 0; i < fileText.size(); i++) {
            System.out.println(fileText.get(i));
            counts += 1;
        }
        System.out.println("一共"+counts+"条时间数据！");
        Date date = new Date();
        SimpleDateFormat sdf =  new SimpleDateFormat();

    }
}